<?php  
class ControllerCommonHome extends Controller {
	public function index() {
    
		$this->document->setTitle($this->config->get('config_title'));
		$this->document->setDescription($this->config->get('config_meta_description'));
        $this->document->setCurrentLocation('首页');
        
		$this->data['heading_title'] = $this->config->get('config_title');
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/home.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/home.tpl';
		} else {
			$this->template = 'default/template/common/home.tpl';
		}
		
        $this->data['dy_breadcrumb'] = 'dasdsa';
        
		$this->children = array(
			'common/footer',
			'common/header'
		);
        
        $this->load->model('account/address');
        
		//user
        $this->data['firstname'] = $this->customer->getFirstName();
        $this->data['logged'] = $this->customer->isLogged();
        $this->data['userid'] = 0;
        $this->data['orderlist'] = "";
        //chech has address
        $this->data['hasAddress'] = 'true';
        $this->data['hasAddressId'] = 0;
        if($this->customer->isLogged()){
            $hasAddressId = $this->customer->getAddressId();
            $this->data['hasAddressId'] = $hasAddressId;
            $hasAddress_results = $this->model_account_address->getAddress($hasAddressId);
            if($hasAddress_results['address_1'] == '' || $hasAddress_results['address_2'] == '' ){
                $this->data['hasAddress'] = 'false';
            }
        }
        
        if($this->customer->isLogged()){
            if(isset($_COOKIE['dy_userid']) && $_COOKIE['dy_userid'] == $this->customer->getId()){
                //selected menu order
                $this->data['orderlist'] = isset($_COOKIE['dy_orderList']) ? $_COOKIE['dy_orderList'] : null;
            }
            $this->data['userid'] = $this->customer->getId();
        }

        //next 7 days
        $this->data['sevendays'] = array();
        for($i =0; $i<8; $i++){
            $this->data['sevendays'][] = date('Y-m-d',time()+86400*$i);
        }
        
        //get dcl
        if ($this->customer->isLogged() && isset($this->session->data['dcl'])) {
			$this->data['dcl'] = $this->session->data['dcl'];		
		}else{
            $this->data['dcl'] = 0;
        }
        
        //get people_num
        if ($this->customer->isLogged() && isset($this->session->data['people_num'])) {
			$this->data['people_num'] = $this->session->data['people_num'];		
		}else{
            $this->data['people_num'] = 1;
        }
        
        //get shipping_date
        if ($this->customer->isLogged() && isset($this->session->data['shipping_date'])) {
			$this->data['shipping_date'] = $this->session->data['shipping_date'];		
		}else{
            $this->data['shipping_date'] = 0;
        }
        
        //get Address
        if ($this->customer->isLogged() && isset($this->session->data['shipping_address_id'])) {
			$this->data['address_id'] = $this->session->data['shipping_address_id'];		
		}else{
            $this->data['address_id'] = 0;
        }
        
        $this->data['addresses'] = array();
		
		$address_results = $this->model_account_address->getAddresses();
        
    	foreach ($address_results as $result) {

      		$this->data['addresses'][] = array(
        		'address_id' => $result['address_id'],
        		'address'    => $result['address_1'].' - 电话:'.$result['address_2']
      		);
    	}
        
        
        
        $this->data['banner'] = $this->getChild('module/banner', array('banner_id'=>'7', 'width'=>990, 'height'=>210));
        
        
        // add category
        $this->language->load('product/category');
		
		$this->load->model('catalog/category');
		
		$this->load->model('catalog/product');

        // get random featured
		$featured_data = array(
            'filter_category_id' => 0, 
            'sort'               => 'rating',
            'order'              => 'desc',
            'start'              => 0,
            'limit'              => 15
        );
        
        $featured_results = $this->model_catalog_product->getProducts($featured_data);
        $this->data['features'] = array();
        foreach ($featured_results as $result) {
            if ($result['image']) {
                $image = $this->model_tool_image->resize($result['image'], 150, 150);
            } else {
                $image = false;
            }
        
            $this->data['features'][] = array(
                'product_id'  => $result['product_id'],
                'thumb'       => $image,
                'name'        => $result['name']
            );
        }		
		
         //get first level cate
        $categories = $this->model_catalog_category->getCategories(68);
        $def_category_id = $categories[0]['category_id'];

        $_path = '';
		if (isset($this->request->get['path'])) {

			$parts = explode('_', (string)$this->request->get['path']);
		
			foreach ($parts as $path_id) {
				if (!$_path) {
					$_path = $path_id;
				} else {
					$_path .= '_' . $path_id;
				}
			}		
		
			$category_id = array_pop($parts);
		} else {
			$category_id = $def_category_id;
		}
		
        $this->data['category_id'] = $category_id;
        $this->data['categories1'] = $categories;
        $this->data['categories2'] = $this->model_catalog_category->getCategories(69);
        /*
        $categories1 = array(
            '0'=> array('name'=>'新品推荐','category_id'=>59),
            '1'=> array('name'=>'特色招牌','category_id'=>60),
            '2'=> array('name'=>'精选套餐','category_id'=>61)
        );
        $categories2 = array(
            '0'=> array('name'=>'豆制品及主食','category_id'=>26),
            '1'=> array('name'=>'鲜蔬灵菇','category_id'=>27),
            '2'=> array('name'=>'肉类','category_id'=>62),
            '3'=> array('name'=>'手工丸滑','category_id'=>63),
            '4'=> array('name'=>'火锅底','category_id'=>64),
            '5'=> array('name'=>'小料','category_id'=>65)
        );
        */
        
        
        $pid = '0';
		if (isset($this->request->get['pid'])) {
			$pid = $this->request->get['pid'];
		}
        $this->data['pid'] = $pid;
        
		$this->response->setOutput($this->render());	
        
	}
}
?>